/** UserAction */
package server.service.user;

import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import domain.exception.ValidationException;
import domain.session.Response;
import domain.user.SystemUser;
import domain.user.User;

/**
Handles actions by users
 * @author jessienewman
 * @version $Revision: 1.0 $
 */
public class UserAction {
	/**
	 * Field userModel.
	 */
	private UserModel userModel;
	
	/**
	 * Constructor for UserAction.
	 */
	public UserAction() {
		userModel = new UserModel();
	}
	
	public Response findUsers(final JSONObject requestJson) {
		JSONObject responseJson = new JSONObject();
		String userQuery = requestJson.getString("query");
		List<User> userList = userModel.findUsers(userQuery);
		JSONArray userArray = new JSONArray();
		
		for (User user : userList) {
			userArray.put(user);
		}
		
		
		responseJson.put("users", userArray);
		Response response = new Response(true, responseJson);
		return response;
	}
	
	/**
	 * Method isValidUsername.
	 * @param username String
	
	 * @return Boolean */
	public Response thisIsValidUsername(final JSONObject requestJson) {
		String name = requestJson.getString("username");
		boolean valid = userModel.thisIsValidUsername(name);
		Response response = new Response();
		response.setSuccessful(valid);
		return response;
	}
	
	/**
	 * Method loginValid.
	 * @param username String
	 * @param password String
	
	 * @return Boolean */
	public Boolean loginValid(String username, String password) {
		return userModel.loginValid(username, password);
	}
	
	/**
	 * Method getUserId.
	 * @param username String
	
	 * @return Integer */
	public Response getUserId(final JSONObject requestJson) {
		String username = requestJson.getString("username");
		Integer uid = userModel.getUserId(username);
		JSONObject responseJson = new JSONObject();
		responseJson.put("userId", uid);
		Response response = new Response(true, responseJson);
		return response;
	}
	
	/**
	 * Method getUser.
	 * @param userId Integer
	
	 * @return User */
	public Response getUser(final JSONObject requestJson) {
		Integer getUserId = requestJson.getInt("userId");
		User user = userModel.getUser(getUserId);
		Response response = new Response(true, ((SystemUser)user).getJSON());
		return response;
	}
	
	public Response modifyName(final JSONObject requestJson) {
		Integer modUserId = requestJson.getInt("userId");
		String nameFirst = requestJson.getString("nameFirst");
		String nameMiddle = requestJson.getString("nameMiddle");
		String nameLast = requestJson.getString("nameLast");
		Response response = new Response();
		try {
			userModel.modifyName(modUserId, nameFirst, nameMiddle, nameLast);
		} catch (ValidationException e) {
			JSONObject responseJson = new JSONObject();
			responseJson.put("msg", e.getExceptionText());
			response.setJSON(responseJson);
			response.setSuccessful(false);
			return response;
		}
		response.setSuccessful(true);
		return response;
	}
	
	public Response setActive(final JSONObject requestJson) {
		Integer activeId = requestJson.getInt("userId");
		Boolean isActive = requestJson.getBoolean("active");
		userModel.setActive(activeId, isActive);
		Response response = new Response();
		response.setSuccessful(true);
		return response;
	}
	
	public Response addUser(final JSONObject requestJson) {
		SystemUser u = new SystemUser(requestJson.getJSONObject("user"));
		String uname = requestJson.getString("username");
		String p1 = requestJson.getString("p1");
		String p2 = requestJson.getString("p2");
		Integer uId = 0;
		Response response = new Response();
		try {
			uId = userModel.addUser(u, uname, p1, p2);
		} catch (ValidationException e) {
			JSONObject responseJson = new JSONObject();
			responseJson.put("msg", e.getExceptionText());
			response.setJSON(responseJson);
			response.setSuccessful(false);
			return response;
		}
		response.setSuccessful(true);
		JSONObject responseJson = new JSONObject();
		responseJson.put("userId", uId);
		response.setJSON(responseJson);
		return response;
	}
	
	/**
	 * super's toString
	
	 * @return super's toString */
	public String toString() {
		return super.toString();
	}
}
